package me.aartikov.flowsample.slice;

import me.aartikov.alligator.DestinationType;
import me.aartikov.alligator.NavigationContext;
import me.aartikov.alligator.NavigationContextBinder;
import me.aartikov.alligator.Navigator;
import me.aartikov.alligator.animations.AnimationData;
import me.aartikov.alligator.animations.SimpleTransitionAnimation;
import me.aartikov.alligator.animations.TransitionAnimation;
import me.aartikov.alligator.helpers.FractionStack;
import me.aartikov.alligator.screenswitchers.FragmentScreenSwitcher;
import me.aartikov.alligator.screenswitchers.ScreenSwitcher;
import me.aartikov.alligator.Screen;
import me.aartikov.flowsample.ResourceTable;
import me.aartikov.flowsample.SampleApplication;

import me.aartikov.flowsample.SampleTransitionAnimationProvider;
import me.aartikov.flowsample.screens.TestFlowScreen;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;

import java.util.List;

public class MainAbilitySlice extends AbilitySlice {

    private Navigator mNavigator = SampleApplication.getNavigator();
    private NavigationContextBinder mNavigationContextBinder = SampleApplication.getNavigationContextBinder();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
    }

    @Override
    public void onActive() {
        super.onActive();
        bindNavigationContext();
        setInitialFragmentIfRequired();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    private void bindNavigationContext() {

        ScreenSwitcher mScreenSwitcher = new FragmentScreenSwitcher(SampleApplication.getNavigationFactory(), ((FractionAbility) getAbility()).getFractionManager(), new FragmentScreenSwitcher.AnimationProvider() {
            @Override
            public TransitionAnimation getAnimation(Screen screenFrom, Screen screenTo, AnimationData animationData) {
                return new SimpleTransitionAnimation(0, 0);
            }
        });

        NavigationContext.Builder builder = new NavigationContext
                .Builder(getAbility(), SampleApplication.getNavigationFactory())
                .fragmentNavigation(((FractionAbility) getAbility()).getFractionManager())
                .flowFragmentNavigation(((FractionAbility) getAbility()).getFractionManager())
                .transitionAnimationProvider(new SampleTransitionAnimationProvider())
                .screenSwitcher(mScreenSwitcher)
                .transitionListener(((transitionType, destinationType, screenClassFrom, screenClassTo) -> {
                    if (destinationType == DestinationType.FLOW_FRAGMENT) {
                        bindNavigationContext();    // rebind NavigationContext because a current flow fragment has been changed.
                    }
                }));

        mNavigationContextBinder.bind(builder.build());
    }

    public void setInitialFragmentIfRequired() {
        List<Screen> screens = FractionStack.getInstance(((FractionAbility) getAbility()).getFractionManager()).getCurrentFragmentScreen(getAbility().toString());
        if (screens.size() == 0 && mNavigator.canExecuteCommandImmediately()) {
            mNavigator.goForward(new TestFlowScreen(1, ResourceTable.Id_flow_fragment_container, getAbility().toString()));
        } else {
            mNavigator.reduce(screens);
        }
    }

    @Override
    protected void onBackPressed() {
        if (!mNavigator.goBack()) {
            super.onBackPressed();
        }
    }
}
